package com.whxy02.tolearn.app.utils;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * Date与字符串的转换工具类
 */
@Component
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class DateUtils {
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    public String DateToStrng(Date date){
        return sdf.format(date);
    }

    public Date StringToDate(String s){
        try {
            return sdf.parse(s);
        } catch (ParseException e) {
            System.out.println("解析Date字符串失败");
            return null;
        }

    }

    public String getYesterday(Date date){
        Calendar calendar = new GregorianCalendar();

        calendar.setTime(date);

        calendar.add(calendar.DATE,-1);

        return sdf.format(calendar.getTime());
    }
}
